/*
 * Copyright (C) 2003, 2004 Rex Ford
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package GamePlay.GameObjects;
import GamePlay.GameEnv;
import GamePlay.Location;
import GamePlay.Collision.*;
import java.awt.Toolkit;
public class Saucer extends GameObject 
{
	int health = 100;
	private double mySpeed=0.05;
	private long lastShot=System.currentTimeMillis();
	private int timeToWait=1000;
	
	public Saucer(GameEnv env) 
	{
		super(env);
		int x = (int) (Math.random() * 900);
		myLoc = new Location(x, -10);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		myImage = toolkit.getImage("images/GOImages/saucer.gif");
		//collision time!
		int HEIGHT=80;
		int WIDTH=80;
		CollisionShape body = new CollisionRect(2,this,new Location(0,0),WIDTH,HEIGHT);
		this.addCollisionShape(body);
	}
	
	public void pulse(long millis) 
	{
		myLoc.moveY(mySpeed*millis);
		if(theEnv.getUser().locate().getY()>myLoc.getY())
		{
			GameObject user = theEnv.getUser();
			Location xmotion=user.locate().subtractLocs(myLoc);
			xmotion=xmotion.scale(0.00035*millis);
			myLoc=myLoc.addLocs(xmotion);
			
			if(user.locate().getX()>myLoc.getX()-20 && 
		   		user.locate().getX()<myLoc.getX()+20 );
					tryShoot();
		}
	}
	
	private void tryShoot()
	{
		if(System.currentTimeMillis()-lastShot>=timeToWait)
		{
			timeToWait=(int)(Math.random()*800)+700;
			lastShot=System.currentTimeMillis();
			Bullet shot = new Bullet(myLoc.addLocs
			         (new Location(myImage.getWidth(null)/2,myImage.getHeight(null))),theEnv,false,2);
			theEnv.addGO(shot,2);
		}	
	}
	
	public void collide(GameObject theGO)
	{
		if(theGO instanceof Bullet)
		{
			damage(((Bullet)theGO).damage);
		}
		else if(theGO instanceof UserShip)
		{
			damage(2);
		}
	}
	
	public void damage(int amount)
	{
		health-= amount;
		if(health<=0){
			removeMe();
			//explode!
			Explosion exp = new Explosion(theEnv,100,myLoc,mySpeed);
			theEnv.addGO(exp,2);
			try
			{
				theEnv.getUser().score++;	//user might be dead...
			}
			catch(ClassCastException e)
			{
				System.out.println("Saucer: I owe a dead user a point!");
			}
		}
	}
}